package Atm01;

import Polymorphic.Student;
import Tz1.Strin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录面板
 */
public class LoginPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private SuperArray superArray=new SuperArray();
    private int inputaccount;




    private ImageIcon loginImg = new ImageIcon("Images/buttonLand.jpg");
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(loginImg);

    private Font font = new Font("黑体",Font.BOLD,30);

    public ATMFrame getAtmFrame() {
        return atmFrame;
    }

    public void setAtmFrame(ATMFrame atmFrame) {
        this.atmFrame = atmFrame;
    }

    public LoginPanel(ATMFrame atmFrame){
        this.setLayout(null);
        this.atmFrame=atmFrame;

        accountLabel.setBounds(300,200,120,50);
        accountLabel.setFont(font);

        accountField.setBounds(400,200,250,50);
        accountField.setFont(font);

        pwdLabel.setBounds(300,260,120,50);
        pwdLabel.setFont(font);

        pwdField.setBounds(400,260,250,50);
        pwdField.setFont(font);

        loginBtn.setBounds(510,320,140,50);
        this.add(accountLabel);
        this.add(accountField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);


        loginBtn.addActionListener(this);


        // 设置当前面板的背景为透明
        this.setOpaque(false);
    }




    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==loginBtn){
            String  account =accountField.getText();
            String pwd=new String(pwdField.getPassword());
            SuperArray users=atmFrame.getUsers();
            for (int i = 0; i <users.size() ; i++) {
              User user = (User)users.get(i);
                if(user.getAccount().equals(account)&&user.getPwd().equals(pwd)){
                    atmFrame.switchPanel("menu");
                    atmFrame.setLoginUser(user);
                    return;
                }
            }
            inputaccount++;
            JOptionPane.showMessageDialog(this,"输入账号密码错误"+(3-inputaccount)+"次");
            if(inputaccount>=3){
                JOptionPane.showMessageDialog(null,"次数用玩。卡被吞");
                System.exit(0);
            }
        }


    }
}
